home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue56 / Splat / widelabel.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-30  |  2.4 KB  |  90 lines

  1. unit WideLabel;
  2.  
  3. interface
  4.  
  5. // TWideLabel: just like TLabel, but supports wide strings.
  6. // Copyright ⌐ 2000 Tempest Software, Inc.
  7.  
  8. uses
  9.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   StdCtrls;
  11.  
  12. type
  13.   TWideLabel = class(TLabel)
  14.   private
  15.     fWideCaption: WideString;
  16.     procedure SetWideCaption(const Value: WideString);
  17.     procedure ReadCaption(Reader: TReader);
  18.     procedure WriteCaption(Writer: TWriter);
  19.   protected
  20.     procedure DoDrawText(var Rect: TRect; Flags: Longint); override;
  21.     procedure DefineProperties(Filer: TFiler); override;
  22.   published
  23.     property Caption: WideString read fWideCaption write SetWideCaption stored False;
  24.   end;
  25.  
  26. implementation
  27.  
  28. { TWideLabel }
  29.  
  30. // Delphi stores a WideString by converting it to a narrow string,
  31. // which discards information. Store the actual WideString data.
  32. procedure TWideLabel.DefineProperties(Filer: TFiler);
  33. begin
  34.   inherited;
  35.   Filer.DefineProperty('WideCaption', ReadCaption, WriteCaption, Caption <> '');
  36. end;
  37.  
  38. // Copied from TLabel.DoDrawText, but changed to use DrawTextW
  39. // insted of DrawText (which is DrawTextA).
  40. procedure TWideLabel.DoDrawText(var Rect: TRect; Flags: Integer);
  41. var
  42.   Text: WideString;
  43. begin
  44.   Text := Caption;
  45.   if (Flags and DT_CALCRECT <> 0) and
  46.      ((Text = '') or
  47.          ShowAccelChar and (Text[1] = '&') and (Text[2] = #0))
  48.   then
  49.      Text := Text + ' ';
  50.  
  51.   if not ShowAccelChar then
  52.     Flags := Flags or DT_NOPREFIX;
  53.   Flags := DrawTextBiDiModeFlags(Flags);
  54.   Canvas.Font := Font;
  55.  
  56.   if not Enabled then
  57.   begin
  58.     OffsetRect(Rect, 1, 1);
  59.     Canvas.Font.Color := clBtnHighlight;
  60.     DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), Rect, Flags);
  61.     OffsetRect(Rect, -1, -1);
  62.     Canvas.Font.Color := clBtnShadow;
  63.     DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), Rect, Flags);
  64.   end
  65.   else
  66.     DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), Rect, Flags);
  67. end;
  68.  
  69. // Read the real caption from the DFM.
  70. procedure TWideLabel.ReadCaption(Reader: TReader);
  71. begin
  72.   fWideCaption := Reader.ReadWideString;
  73. end;
  74.  
  75. // Store a new caption and redraw the control.
  76. procedure TWideLabel.SetWideCaption(const Value: WideString);
  77. begin
  78.   fWideCaption := Value;
  79.   AdjustBounds;
  80.   Invalidate;
  81. end;
  82.  
  83. // Write the real caption to the DFM.
  84. procedure TWideLabel.WriteCaption(Writer: TWriter);
  85. begin
  86.   Writer.WriteWideString(Caption);
  87. end;
  88.  
  89. end.
  90.